package com.swf.common.i18n;

import java.util.*;

import org.dom4j.*;
import org.slf4j.*;

import com.swf.common.exception.*;
import com.swf.common.utils.*;

public class ResourceVisitor extends VisitorSupport {
	private Map<String,Resource> resourceMap = new HashMap<String,Resource>();
	private String resourceId;
	
	private static Logger logger = LoggerFactory
			.getLogger(ResourceVisitor.class);
	
	
	public ResourceVisitor() {
	}

	@Override
	public void visit(Element element) {
		Resource resource = new Resource();
		String labelName =FwsStringUtils.nvl(element.getName());
		if (labelName.equalsIgnoreCase("resource")){
			resourceId=element.attributeValue("id");
			resource.setName(resourceId);
			Iterator<Element> iterator = element.elementIterator("resourceValue");
			while (iterator.hasNext()){
				Element resourceValue = iterator.next();
				String language = resourceValue.attributeValue("id");
				String value = resourceValue.getTextTrim();
				resource.addLanguage(language, value);
			}
			if (FwsStringUtils.isNotBlank(resourceId)){
				resourceMap.put(resourceId, resource);
			}
		}
	}
	
	public Map<String, Resource> getResourceMap() {
		return resourceMap;
	}
	
}

